﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomahawk.Runtime;
using Tomahawk.Runtime.Logic;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Components;
using Tomahawk.Runtime.Rendering.Materials;
using Tomahawk.Runtime.Rendering;
using Microsoft.Xna.Framework.Audio;

namespace Initialization.GameStates
{
    public class TeapotActor : XActor
    {
        protected Cue machineryCue = null;
        public Cue MachineryCue { get { return machineryCue; } }

        public TeapotActor()
        {
            UpdateRequired = true;
        }

        protected override void OnPostBeginPlay()
        {
            machineryCue = GetComponent<XAudioEmitterComponent>().Play("Test@machinery");
        }

        protected override void OnUpdate(float elapsedTime)
        {
            Quaternion delta = Quaternion.CreateFromAxisAngle(Vector3.Forward, elapsedTime);
            this.SetRotation(this.SceneComponent.SceneNode.Rotation * delta);
            base.OnUpdate(elapsedTime);
        }
    }

    public class MusicActor : XActor
    {
        public MusicActor()
        {
        }

        protected override void OnPostBeginPlay()
        {
            GetComponent<XAudioEmitterComponent>().Play("Test@fanfare");
        }
    }

    public class ListenerActor : XActor
    {
        public ListenerActor()
        {
            UpdateRequired = true;
        }

        protected override void OnPostBeginPlay()
        {
            XAudioListenerComponent listener = GetComponent<XAudioListenerComponent>();
            listener.Enabled = true;
        }
        
        protected override void OnUpdate(float elapsedTime)
        {
            Tomahawk.Runtime.Rendering.Camera camera = Engine.Instance.CameraManager.ActiveCamera;
            this.SetLocation(camera.EyePosition);
            this.SetRotation(Quaternion.CreateFromRotationMatrix(camera.ViewMatrix));

            base.OnUpdate(elapsedTime);
        }
    }

    class BasicGameState : GameState
    {
        public TeapotActor teapot = null;
        public MusicActor music = null;
        public ListenerActor listener = null;

        protected override void OnLoad()
        {
            teapot = (TeapotActor)XWorld.Instance.CreateObject(typeof(TeapotActor), "", @"Examples.Audio.Contents\teapotactor.xml");
            music = (MusicActor)XWorld.Instance.CreateObject(typeof(MusicActor), "", @"Examples.Audio.Contents\musicactor.xml");
            listener = (ListenerActor)XWorld.Instance.CreateObject(typeof(ListenerActor), "", @"Examples.Audio.Contents\listeneractor.xml");

            Engine.Instance.ResourceManager.LoadPendantResources(this.ContentManager);
        }

        protected override void OnUnload()
        {
        }

        protected override void OnInitialize()
        {
            Engine.Instance.CameraManager.SetActiveCamera(FreeCamComponent.label);
            Tomahawk.Runtime.Rendering.Camera camera = Engine.Instance.CameraManager.ActiveCamera;
            camera.LookAt(
                new Vector3(-400, 0, 0),
                new Vector3(0, 0, 0),
                Vector3.UnitY);
        }

        protected override void OnDestroy()
        {
        }
    }
}
